Part 4: Assembling the Program

Finally, create a main loop to process the input commands.
  • The program starts by initializing an empty tree (root = None).
  • It reads the number of operations, then enters a loop to process each command one by one.
  • Inside the loop, it parses the operation type and key from the input line.
  • Based on the operation, it calls the appropriate function (insert, find, succ, or pred).
  • Handle return values carefully: insert may change the root, and successor/predecessor should print "null" when no value exists.
# --- Main Program Logic ---
root = __________
num_operations = int(input())

for _ in range(num_operations):
    line = input().strip().split()
    op, key = line[0], int(line[1])

    if op == "ins":
        root = __________(root, key)
    elif op == "find":
        result = __________(root, key)
        print(str(result).lower())
    elif op == "succ":
        result = __________(root, key)
        print(__________ if __________ is not __________ else "__________")
    elif op == "pred":
        # TODO: Call predecessor and print result
        result = __________(root, key)
        print(__________ if __________ is not __________ else "__________")
                
Copied!